/**
 * 
 */
package be.gwi.alfresco.run.helper.impl;

import org.springframework.util.Assert;

import be.gwi.alfresco.run.config.RunConfig;
import be.gwi.alfresco.run.helper.RunHelper;
import be.gwi.alfresco.run.model.Run;
import be.gwi.alfresco.run.model.Runner;

/**
 * Runner helper will provide helper methods about runners
 * 
 * @author gabriel.wilems@gmail.com
 * 
 * @see RunHelper
 * @version 1.0
 * @since 1.0
 */
public class RunHelperImpl implements RunHelper {

    private RunConfig runConfig;

    /**
     * Initializes the helper.
     * 
     * Verifies that all the attributes have been set.
     * 
     * @throws IllegalArgumentException
     *             if not all attributes have been set.
     */
    public void init() {
        Assert.notNull(this.runConfig, "The run configuration must have been set");
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see
     * be.gwi.alfresco.run.helper.RunnerHelper#addRunToRunner(be.gwi.alfresco
     * .run.model.Run, be.gwi.alfresco.run.model.Runner)
     */
    public void addRunToRunner(Run run, Runner runner) {
        switch (run.getRunType()) {
        case RUNNING:
            runner.setRunningDistance(run.getRunDistance() + runner.getRunningDistance());
            runner.setRunningCount(runner.getRunningCount() + 1);
            runner.setRunPoint(runner.getRunPoint() + (run.getRunDistance() * runConfig.getRunningPoint()));
            break;
        case CYCLING:
            runner.setCyclingDistance(run.getRunDistance() + runner.getCyclingDistance());
            runner.setCyclingCount(runner.getCyclingCount() + 1);
            runner.setRunPoint(runner.getRunPoint() + (run.getRunDistance() * runConfig.getCyclingPoint()));
            break;
        case SWIMMING:
            runner.setSwimmingDistance(run.getRunDistance() + runner.getSwimmingDistance());
            runner.setSwimmingCount(runner.getSwimmingCount() + 1);
            runner.setRunPoint(runner.getRunPoint() + (run.getRunDistance() * runConfig.getSwimmingPoint()));
        }
        runner.setRunCount(runner.getRunCount() + 1);
    }

    /**
     * @param runConfig
     *            the runConfig to set
     */
    public void setRunConfig(RunConfig runConfig) {
        this.runConfig = runConfig;
    }
}
